package com.molyfun.parents.activity.classroom;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.molyfun.parents.R;
import com.molyfun.parents.activity.course.CourseDetailActivity;
import com.molyfun.parents.rest.bean.CourseListResponse;
import com.squareup.picasso.Picasso;

import butterknife.ButterKnife;
import butterknife.InjectView;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by william(liuchao.hit@gmail.com).
 * 列表内的展示和外面不用
 * 标题要大写
 */
public class ClassCourseListView extends LinearLayout implements View.OnClickListener {

    protected LayoutInflater layoutInflater;
    private String TAG = "CourseView";
    private Context context;
    private boolean inited;

    @InjectView(R.id.course_name)
    TextView mCourseName;
    @InjectView(R.id.course_target)
    TextView mCourseTarget;
    @InjectView(R.id.avator)
    CircleImageView mAcator;
    @InjectView(R.id.user_name)
    TextView mUserName;
    @InjectView(R.id.group_name)
    TextView mGroupName;

    CourseListResponse.CourseBean mCourseBean;

    public ClassCourseListView(Context context, AttributeSet attrs, int defStyle) {
        this(context, attrs);
    }

    public ClassCourseListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public ClassCourseListView(Context context) {
        super(context);
        init(context, null);
    }


    private void init(Context context, AttributeSet attrs) {
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
        layoutInflater.inflate(R.layout.widget_list_class_course, this);
        ButterKnife.inject(this);
    }

    /**
     * init data
     *
     * @param courseBean courseModel
     */
    public void initData(CourseListResponse.CourseBean courseBean) {

        if (courseBean == null) {
            return;
        }

        mCourseBean = courseBean;
        mCourseName.setText(courseBean.getCoursename());
        mCourseName.setOnClickListener(this);
        mCourseTarget.setText(courseBean.getCoursetarget());

        if (!TextUtils.isEmpty(courseBean.getHeadimgurl())) {
            Picasso.with(context)
                    .load(courseBean.getHeadimgurl())
                    .placeholder(R.mipmap.ic_header_default)
                    .into(mAcator);
        }else{
            mAcator.setImageResource(R.mipmap.ic_header_default);
        }

        mUserName.setText(courseBean.getUsername());
        mGroupName.setText(courseBean.getGroupname());
        setOnClickListener(this);

    }

    public void setOnLongClickListener(OnLongClickListener onLongClickListener) {
        super.setOnLongClickListener(onLongClickListener);
    }


    @Override
    public void onClick(View v) {
        if (mCourseBean != null) {
            CourseDetailActivity.startActivity(context, mCourseBean, true);
        }
    }

}